package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BaseException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;


    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        // 设置 创建时间
        statement.setCreateTime(new Date());
        // 消费状态应该为 消费中
       if (statement.getStatus().equals(Statement.STATUS_PAID)){
            throw new  BaseException("非法操作");
        }
        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        // 变修逻辑： 只要在消费中的状态才可以编辑
        Statement temp = statementMapper.selectByPrimaryKey(statement.getId());
        if (temp != null && temp.getStatus().equals(Statement.STATUS_PAID)){
            throw new BaseException("已支付状态不能编辑");
        }
        // 编辑时，注意是群全列修改 还是 部分列修改，这里调用的是全列修改，这里注意创建时间的修改
        statement.setCreateTime(temp.getCreateTime());
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public Statement selectByAppointmentId(Long id) {

        return statementMapper.selectByAppointmentId(id);
    }

    @Override
    public void deleteStatement(Long id) {
        //思路逻辑： 删除结算单本身，还要删除结算单 + 对应的结算单明细 + 把其对应的预约中的状态为已到店

        // 1 先把结算单查出来（注意，这不需要删除结算单之前查）
        Statement statement = statementMapper.selectByPrimaryKey(id);
        // 2 删除结算
        statementMapper.deleteByPrimaryKey(id);
        // 3 删除结算对应的明细
        statementItemMapper.deleteByStatementId(id);
        // 4 修改结算单对应的预约单，把其状态改为 已到店
        appointmentMapper.updateStatus(statement.getAppointmentId(), Appointment.STATUS_ARRIVAL);
    }

    @Override
    public void updateAmount(Long id, BigDecimal itemPrice, BigDecimal totalAmount, BigDecimal totalQuantity) {
        statementMapper.updateAmount(id, itemPrice, totalAmount, totalQuantity);
    }

    @Override
    public void payStatement(Statement statement) {
        statementMapper.updateStatement(statement);
    }




}
